package com.dcjt.firehotcarpad.model

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.aries.library.fast.manager.LoggerManager
import com.aries.library.fast.retrofit.FastLoadingObserver
import com.aries.library.fast.retrofit.FastObserver
import com.aries.library.fast.util.FastStackUtil
import com.aries.library.fast.util.ToastUtil
import com.dcjt.firehotcarpad.entity.BiaohuiEntity
import com.dcjt.firehotcarpad.entity.CarZZXX
import com.dcjt.firehotcarpad.entity.MapEvent
import com.dcjt.firehotcarpad.entity.Template
import com.dcjt.firehotcarpad.retrofit1.repository.ApiRepository1

/**
 * Created by cj on 2021/2/22.
 * Email:codesexy@163.com
 * Function:
 * desc:警情处理界面的viewModel
 */
class HandleDisasterViewModel : ViewModel() {


    var infoLivaData: MutableLiveData<CarZZXX> = MutableLiveData()
    //标绘列表
    var biaohuiLivaData: MutableLiveData<List<BiaohuiEntity>> = MutableLiveData()

    //地图上添加marker
    private var mapControlLiveData: MutableLiveData<MapEvent> = MutableLiveData()

    //模版
    private var templates: MutableList<Template>? = null

    private var jqid: String = ""


    /**
     * 拉取警情详情
     */
    fun loadDisasterDetail(disasterId: String) {
        ApiRepository1.getInstance().getCarZuozhanXX(disasterId)
            .subscribe(object : FastLoadingObserver<CarZZXX>(FastStackUtil.getInstance().current) {

                override fun _onError(errorRes: Int, errorCode: Int, e: Throwable) {
                    super._onError(errorRes, errorCode, e)
                    ToastUtil.show("获取车辆作战信息失败")
                    e.printStackTrace()
                }

                override fun _onNext(entity: CarZZXX) {
                    if (entity != null) {
                        LoggerManager.e("LocationType_getNet")
                        infoLivaData.postValue(entity)
                    } else {
                        ToastUtil.show("获取车辆作战信息失败")
                    }
                }
            })
    }

    /**
     * 拉取标绘列表
     */
    fun loadBiaohuiList(zqid: String,ssbhid: String) {
        ApiRepository1.getInstance().getBiaohuiList(zqid,ssbhid)
            .subscribe(object : FastLoadingObserver<List<BiaohuiEntity>>(FastStackUtil.getInstance().current) {

                override fun _onError(errorRes: Int, errorCode: Int, e: Throwable) {
                    super._onError(errorRes, errorCode, e)
                    ToastUtil.show("获取标绘列表失败")
                    e.printStackTrace()
                }

                override fun _onNext(entity: List<BiaohuiEntity>) {
                    if (entity != null) {
                        biaohuiLivaData.postValue(entity)
                        if (entity.size == 0){
                            ToastUtil.show("当前无标绘信息")
                        }
                    } else {
                        ToastUtil.show("当前无标绘信息")
                    }
                }
            })
    }


    fun getTemplate() {
        ApiRepository1.getInstance().templates
            .subscribe(object : FastObserver<List<Template>>() {

                override fun _onError(errorRes: Int, errorCode: Int, e: Throwable) {
                    super._onError(errorRes, errorCode, e)
                    e.printStackTrace()
                    LoggerManager.e("HandleDisasterViewModel", "获取模版错误")
                }

                override fun _onNext(entity: List<Template>) {
                    templates = mutableListOf()
                    templates!!.addAll(entity)
                }
            })
    }


    fun getDisasterInfo(): LiveData<CarZZXX> = infoLivaData

    /**
     * 发送地图事件
     */
    fun getMapControl(): MutableLiveData<MapEvent> = mapControlLiveData

    /**
     * 获取模版
     */
    fun getTemplates(): MutableList<Template>? = templates

    fun setJqid(jqid: String) {
        this.jqid = jqid
    }

    fun getJqid():String{ return jqid}
}